package com.mzj.solr;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;

public class SolrMove {
	
	private static final Logger LOG = Logger.getLogger(SolrMove.class);  
	
	public static void move(SolrClient client ,SolrClient cloudClient, 
			String index,Integer start) throws Exception {
		SolrQuery query = new SolrQuery("*:*");  
		query.setRows(10000);
		query.setStart(start);
		LOG.info("--------------query: start,"+start+" end,"+(start+10000));
		//query.addSort("create_time", ORDER.asc);
        QueryResponse resp = client.query(query);  
        
        int size = resp.getResults().size();
        SolrDocumentList list = resp.getResults();
        List<SolrInputDocument> inpList = new ArrayList<SolrInputDocument>();
        for(int i=0; i<size; i++) {
        	SolrDocument doc = list.get(i);
        	Collection<String> ks = doc.getFieldNames();
        	SolrInputDocument input = new SolrInputDocument();
        	for (String k : ks) {
        		if(!"_version_".equals(k)){
        			input.addField(k, doc.getFieldValue(k));
        		}
        	}  
        	inpList.add(input);
        }

        cloudClient.add(index,inpList);
        cloudClient.commit(index);
	}
	
	public static Long getTotal(SolrClient client, String index) {
		SolrQuery query = new SolrQuery("*:*");  
		query.setFilterQueries("req_time:[20180103000000 TO 20180104095959]");
		query.setRows(1);
		query.setStart(0);
        QueryResponse resp;
		try {
			resp = client.query(query);
			return resp.getResults().getNumFound();
		} catch (Exception e) {
			LOG.error(e.getMessage(),e);
		}  
		return 0L;
	}
	
}
